/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import NewsBlur 1.0
import "../../../../shared/qml/UIConstants.js" as UIConstants
import "../../../../shared/qml" as Shared

CommonNewsBlurSheet {
    id: root
    property string __originalName: ""
    acceptButtonText: qsTr("Rename")
    rejectButtonText: qsTr("Cancel")
    busyText: qsTr("Renaming")


    function updateAcceptButtonStatus() {
        root.acceptButton.enabled = nameField.text.length;
    }

    Connections {
        target: root.service
        onItemRenamingFinished: root.onItemRenamingFinished(result)
    }

    function onItemRenamingFinished(result) {

        if((result == NewsBlurEnum.SreNoError)
            || result == NewsBlurEnum.SreAborted) {
            root.close();
            return;
        }
        root.busy = false;
        if(result == NewsBlurEnum.SreNetworkError)
            root.showMessage(qsTr("Network error"));
        else if(root.pageItem.itemType == NewsBlurItem.FeedItem)
            root.showMessage(qsTr("Failed to rename the feed"));
        else
            root.showMessage(qsTr("Failed to rename the folder"));
    }

    onAccepted: {
        var newName = nameField.text.trim();
        if(newName.length) {
            if(newName != root.__originalName) {
                root.busy = true;
                root.open();
                root.service.renameItem(root.pageItem, newName);
            } else {
                root.open();
                root.showMessage(qsTr("A different name please"));
            }
        } else {
            root.open();
            root.showMessage(qsTr("Name please"));
        }
    }

    onRejected: {
        if(root.busy) {
            root.open();
            root.service.abortItemRename(root.pageItem);
        }
    }

    onPageItemChanged: {
        if(root.pageItem) {
            nameField.text = pageItem.title;
            root.__originalName = pageItem.title
        }
    }

    contentItem: Column {
        anchors.fill: parent
        anchors.leftMargin: UIConstants.DEFAULT_MARGIN
        anchors.rightMargin: UIConstants.DEFAULT_MARGIN
        spacing: UIConstants.DEFAULT_HALF_MARGIN

        Label {
            text: qsTr("Name")
            font.family: UIConstants.FONT_FAMILY_LIGHT
        }
        Shared.TextField {
            id: nameField
            width: parent.width
            inputMethodHints: Qt.ImhNoPredictiveText
            onTextChanged: root.updateAcceptButtonStatus();
            platformSipAttributes: SipAttributes {
                actionKeyEnabled: root.acceptButton.enabled
                actionKeyLabel: root.acceptButtonText
                actionKeyHighlighted: true
            }
            Keys.onReturnPressed: {
                if(root.acceptButton.enabled) {
                    root.accept();
                }
            }
        }
        Connections {
            target: root
            onStatusChanged: {
                if(root.status == DialogStatus.Open) {
                    nameField.forceActiveFocus();
                    nameField.platformOpenSoftwareInputPanel();
                }
            }
        }
    }
}
